package com.youi.shop.controller;


import com.youi.shop.commons.exceptions.GlobalException;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.pojo.entity.CompanyAddress;
import com.youi.shop.pojo.qo.address.AddressQO;
import com.youi.shop.pojo.vo.address.MemberAddressVO;
import com.youi.shop.pojo.vo.address.UserAddressVO;
import com.youi.shop.service.CompanyAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/companyAddress")
@Api(tags = "地址业务接口")
public class CompanyAddressController {

    @Resource
    CompanyAddressService companyAddressService;

    /**
     * 根据会员ID查询地址信息
     *
     * @param memberId
     * @return
     */
    @ApiOperation("根据会员ID查询地址信息")
    @GetMapping("/memberAddress/{memberId}")
    public RespEntity<List<MemberAddressVO>> selectByMemberId(@PathVariable Long memberId) {

        return companyAddressService.selectByMemberId(memberId);
    }

    /**
     * 根据商家用户ID查询地址信息
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据商家用户ID查询地址信息")
    @GetMapping("/userAddress/{userId}")
    public RespEntity<List<UserAddressVO>> selectByUserId(@PathVariable Long userId) {

        return companyAddressService.selectByUserId(userId);
    }

    /**
     * 添加地址信息
     * @param addressQO
     * @return
     */
    @ApiOperation("添加地址信息")
    @PostMapping("/addAddress")
    public RespEntity<Object> saveAddressByMemberId(AddressQO addressQO) {
        boolean flag = companyAddressService.insert(addressQO);
        if (!flag) {
            throw new GlobalException(StatusCode.ADDRESS_INSERT_ERROR);
        }
        return RespEntity.success(null);
    }

    /**
     * 会员修改地址信息
     * @param addressQO
     * @return
     */
    @ApiOperation("会员修改地址信息")
    @PostMapping("/MemberUpdateAddress")
    public RespEntity<MemberAddressVO> MemberUpdate(AddressQO addressQO) {
        CompanyAddress address = companyAddressService.updateById(addressQO);
        if (address ==null) {
            throw new GlobalException(StatusCode.ADDRESS_UPDATE_ERROR);
        }
        MemberAddressVO memberAddressVO = new MemberAddressVO();
        BeanUtils.copyProperties(address, memberAddressVO);
        return RespEntity.success(memberAddressVO);
    }

    /**
     * 商家修改地址信息
     * @param addressQO
     * @return
     */
    @ApiOperation("商家修改地址信息")
    @PostMapping("/UserUpdateAddress")
    public RespEntity<UserAddressVO> Userupdate(AddressQO addressQO) {
        CompanyAddress address = companyAddressService.updateById(addressQO);
        if (address ==null) {
            throw new GlobalException(StatusCode.ADDRESS_UPDATE_ERROR);
        }
        UserAddressVO userAddressVO = new UserAddressVO();
        BeanUtils.copyProperties(address, userAddressVO);
        return RespEntity.success(userAddressVO);
    }


    @ApiOperation("根据地址ID删除地址信息")
    @PostMapping("/deleteAddress")
    public RespEntity<Object> delete(@RequestParam List<Long> addressIds) {
        boolean flag = companyAddressService.deleteByIds(addressIds);
        if (!flag) {
            throw new GlobalException(StatusCode.ADDRESS_DELETE_ERROR);
        }
        return RespEntity.success(null);
    }








}
